package com.etl.runner;

import com.etl.extract.DatabaseBuilder;
import com.etl.sys.service.ISysLockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.TimeUnit;

/**
 * 数据库初始化运行类，生成加载数据库定时器
 *
 * @author: chenfenghai
 * @version: 1.0
 * @date: 2020-10-14 9:38
 */
@Slf4j
@Order(2)
@Component
public class DatabaseRunner implements ApplicationRunner {

    @Autowired
    DatabaseBuilder databaseBuilder;

    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;

    @Override
    public void run(ApplicationArguments args) throws InterruptedException {
        TimeUnit.SECONDS.sleep(30);
        threadPoolTaskScheduler.scheduleWithFixedDelay(
                databaseBuilder::loadDatabaseTimer,
                Instant.now().plusSeconds(ISysLockService.LOCK_SECOND),
                Duration.ofMinutes(2));
    }
}
